package com.xiaou.eChartsDemo;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.data.Data;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Gauge;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.SeriesFactory;
import com.xiaou.entity.Bbb;
import com.xiaou.mapper.BbbMapper;
import com.xiaou.vo.PreMap;
/**
 * echarys 使用echarys的例子
 * @author xiaou
 *
 */
@Controller
@RequestMapping("/echarys")
public class ECharys1Controller {
	
	@RequestMapping("/{demo}")
	public String demo1(@PathVariable String demo) {
		return demo;
	}
	/**
	 * 自己封装数据
	 * @return
	 * @throws InterruptedException
	 */
	@RequestMapping("/d2")
	public @ResponseBody Map<String, List<?>> d2() throws InterruptedException{
		Map<String, List<?>> map = new HashMap<>();
		List<Integer> list = new ArrayList<Integer>();
		list.add(100);
		list.add(200);
		list.add(300);
		list.add(400);
		list.add(500);
		map.put("data", list);
		List<String> list2 = new ArrayList<String>();
		list2.add("1");
		list2.add("2");
		list2.add("3");
		list2.add("4");
		list2.add("5");
		map.put("Xaxis", list2);
		return map;
	}
	
	
	
	/**
	 * 自己封装数据
	 * @return
	 */
	@RequestMapping("/d3")
	public @ResponseBody Map<String, Object> d3(){
		Map<String, Object> map = new HashMap<>();
		List<PreMap> list2 = new ArrayList<>();
		list2.add(new PreMap(335,"xjh"));
		list2.add(new PreMap(310,"邮件营销"));
		list2.add(new PreMap(234,"联盟广告"));
		list2.add(new PreMap(135,"视频广告"));
		list2.add(new PreMap(1548,"搜索引擎"));
		map.put("legend",new String[]{"直接访问","邮件营销","联盟广告","视频广告","搜索引擎"});
		map.put("data",list2);
		return map;
	}
	/**
	 * 返回一整个option到前端
	 * 柱状图和折线图合体
	 * @return
	 */
	@RequestMapping("/t1")
	public @ResponseBody Option t1() {
		Option option = new Option();
		option.title("ECharts 入门示例").legend("销量","销量曲线");
		CategoryAxis categoryAxis = new CategoryAxis().data("iPhone4s","iPhone5s","iPhone6","iPhone7","iPhoneX");
		option.xAxis(categoryAxis);
		option.yAxis(new ValueAxis());
		Bar bar = SeriesFactory.newBar("销量").data(100, 500, 300, 200, 1000);
		Line line = SeriesFactory.newLine("销量曲线").data(100, 500, 300, 200, 1000);
		
		line.itemStyle().normal().color("PINK");
		option.series(bar);
		option.series(line);
		return option;
	}
	/**
	 * 
	 * 前端不能使用getJSON
	 * 使用ajax之类的操作来代替
	 * @return
	 */
	@RequestMapping("/t2")
	public @ResponseBody Option t2() {
		Option option = new Option();
		option.title("ECharts 入门示例").legend("销量","销量曲线");
		option.yAxis(new ValueAxis());
		Bar bar = SeriesFactory.newBar("销量").data(100, 500, 300, 200, 1000);
		option.series(bar);
		return option;
	}
	
	
	/**
	 * 仪表盘
	 */
	@RequestMapping("/t3")
	public @ResponseBody Option  t3(){
		Option option = new Option();
		option.tooltip().formatter("{a} <br/>{b} : {c}%");
		Gauge gauge = SeriesFactory.newGauge("CPU");
		gauge.detail().formatter("{value}%");
		gauge.data(new Data("使用率", new Random().nextInt(100)));
		option.series(gauge);
		return option;
	}
	/**
	 * 尝试从数据库读取数据
	 * 并动态显示到页面
	 */
	@Autowired
	private BbbMapper bbbMapper;
	@RequestMapping("/t4")
	public @ResponseBody Option t4() {
		
		List<Bbb> bbbList = bbbMapper.selectALLBbb();
		
		Option option = new Option();
		option.title("ECharts 入门示例").legend("销量","销量曲线");
		option.xAxis(new ValueAxis());
		CategoryAxis categoryAxis = new CategoryAxis();
		Bar bar = SeriesFactory.newBar("销量");
		for(Bbb bbb : bbbList) {
			categoryAxis.data(bbb.getName());
			bar.data(bbb.getValue());
		}
		option.series(bar);
		option.yAxis(categoryAxis);
		return option;
	}
	
	/**
	 * 异步加载,实现数据的动态更新
	 */
	@RequestMapping("/t5")
	public @ResponseBody Map<String, Integer> t5() {
		Map<String, Integer> map = new HashMap<>();
		map.put("data", new Random().nextInt(500));
		return map;
	}
	
}
